﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class PositionDutyDtlsDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public PositionDutyDtlsDAL()
        {

        }
        public PositionDutyDtlsInfo GetPositionDutyDtlsInfo(Nullable<int> seq)
        {
            string strSQL;
            PositionDutyDtlsInfo Info = new PositionDutyDtlsInfo();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@Seq", seq);
                dbManager.AddParameters(1, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT dt.POS_DUTY_DTLS_SEQ, dt.POS_DUTY_SEQ, dt.SEQ_NO, dt.DESCR, dt.REMARKS"
                        + ", pd.POS_SEQ, pd.DUTY_SEQ, p.POS_NAME, d.DUTY_NAME"
                    + " FROM HRM_MP_POSITION_DUTY_DTLS dt"
                        + " LEFT JOIN HRM_MP_POSITION_DUTIES pd ON dt.POS_DUTY_SEQ = pd.POS_DUTY_SEQ"
                        + " LEFT JOIN HRM_MP_DUTIES d ON pd.DUTY_SEQ = d.DUTY_SEQ"
                        + " LEFT JOIN HRM_MP_POSITIONS p ON pd.POS_SEQ = p.POS_SEQ"
                    + " WHERE dt.RECORD_STATUS not like @DeleteRecord"
                        + " AND dt.POS_DUTY_DTLS_SEQ = @Seq";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    Info.Seq = rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0);
                    Info.PositionDutySeq = rdr.IsDBNull(1) ? (int?)null : rdr.GetInt32(1);
                    Info.No = rdr.IsDBNull(2) ? "" : rdr.GetString(2);
                    Info.Descr = rdr.IsDBNull(3) ? "" : rdr.GetString(3);
                    Info.Remarks = rdr.IsDBNull(4) ? "" : rdr.GetString(4);
                    Info.PositionSeq = rdr.IsDBNull(5) ? (int?)null : rdr.GetInt32(5);
                    Info.DutySeq = rdr.IsDBNull(6) ? (int?)null : rdr.GetInt32(6);
                    Info.PositionName = rdr.IsDBNull(7) ? "" : rdr.GetString(7);
                    Info.DutyName = rdr.IsDBNull(8) ? "" : rdr.GetString(8);
                }

                return Info;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
              
                dbManager.Dispose();
            }
        }
        public IList<PositionDutyDtlsInfo> LoadGridPositionDutyDtls(Nullable<int> PosSeq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<PositionDutyDtlsInfo> infoList = new List<PositionDutyDtlsInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@PosSeq", PosSeq);
                dbManager.AddParameters(1, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT dt.POS_DUTY_DTLS_SEQ, dt.POS_DUTY_SEQ, dt.SEQ_NO, dt.DESCR, dt.REMARKS"
                        + ", pd.POS_SEQ, pd.DUTY_SEQ, p.POS_NAME, d.DUTY_NAME"
                    + " FROM HRM_MP_POSITION_DUTY_DTLS dt"
                        + " LEFT JOIN HRM_MP_POSITION_DUTIES pd ON dt.POS_DUTY_SEQ = pd.POS_DUTY_SEQ"
                        + " LEFT JOIN HRM_MP_DUTIES d ON pd.DUTY_SEQ = d.DUTY_SEQ"
                        + " LEFT JOIN HRM_MP_POSITIONS p ON pd.POS_SEQ = p.POS_SEQ"
                    + " WHERE dt.RECORD_STATUS not like @DeleteRecord"
                        + " AND pd.POS_SEQ = @PosSeq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        PositionDutyDtlsInfo info = new PositionDutyDtlsInfo();
                        info.Seq = Convert.IsDBNull(dr[i]["POS_DUTY_DTLS_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["POS_DUTY_DTLS_SEQ"].ToString());
                        info.PositionDutySeq = Convert.IsDBNull(dr[i]["POS_DUTY_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["POS_DUTY_SEQ"].ToString());
                        info.PositionSeq = Convert.IsDBNull(dr[i]["POS_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["POS_SEQ"].ToString());
                        info.DutySeq = Convert.IsDBNull(dr[i]["DUTY_SEQ"]) ? (int?)null : Int32.Parse(dr[i]["DUTY_SEQ"].ToString());
                        info.No = Convert.IsDBNull(dr[i]["SEQ_NO"]) ? "" : (dr[i]["SEQ_NO"]).ToString();
                        info.Descr = Convert.IsDBNull(dr[i]["DESCR"]) ? "" : (dr[i]["DESCR"]).ToString();
                        info.PositionName = Convert.IsDBNull(dr[i]["POS_NAME"]) ? "" : (dr[i]["POS_NAME"]).ToString();
                        info.DutyName = Convert.IsDBNull(dr[i]["DUTY_NAME"]) ? "" : (dr[i]["DUTY_NAME"]).ToString();
                        info.Remarks = Convert.IsDBNull(dr[i]["REMARKS"]) ? "" : (dr[i]["REMARKS"]).ToString();

                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
            return infoList;
        }

        public void Insert(PositionDutyDtlsInfo Info)
        {
            DataSet ds = null;
            DataRowCollection dr = null;

            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@PosSeq", Info.PositionSeq);
                dbManager.AddParameters(1, "@DutySeq", Info.DutySeq);
                dbManager.AddParameters(2, "@No", Info.No);
                dbManager.AddParameters(3, "@Descr", Info.Descr);
                dbManager.AddParameters(4, "@Remarks", Info.Remarks);
                dbManager.AddParameters(5, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(6, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(7, "@CreatedWhen", DateTime.Today);

                strSQL = "SELECT POS_DUTY_SEQ"
                    + " FROM HRM_MP_POSITION_DUTIES"
                    + " WHERE POS_SEQ = @PosSeq"
                        + " AND DUTY_SEQ = @DutySeq"
                        + " AND RECORD_STATUS = @RecordStatus";
                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count <= 0)
                {
                    strSQL = "INSERT INTO HRM_MP_POSITION_DUTIES(POS_SEQ, DUTY_SEQ"
                            + ", REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                        + " VALUES(@PosSeq, @DutySeq, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                    dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                }

                strSQL = "INSERT INTO HRM_MP_POSITION_DUTY_DTLS(POS_DUTY_SEQ, SEQ_NO"
                        + ", DESCR, REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " select pos_duty_seq, @No"                            
                        + ", @Descr, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen"
                    + " from hrm_mp_position_duties"
                        + " where pos_seq = @PosSeq"
                            + " and duty_seq = @DutySeq"
                            + " and record_status = @RecordStatus";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(PositionDutyDtlsInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@Seq", Info.Seq);
                dbManager.AddParameters(1, "@PosDutySeq", Info.PositionDutySeq);
                dbManager.AddParameters(2, "@No", Info.No);
                dbManager.AddParameters(3, "@Descr", Info.Descr);
                dbManager.AddParameters(4, "@Remarks", Info.Remarks);
                dbManager.AddParameters(5, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(6, "@UpdatedWhen", DateTime.Today);
                dbManager.AddParameters(7, "@DutySeq", Info.DutySeq);

                strSQL = "UPDATE HRM_MP_POSITION_DUTIES"
                            + " SET DUTY_SEQ = @DutySeq"
                            + " WHERE POS_DUTY_SEQ = (select pos_duty_seq"
                                + " from hrm_mp_position_duty_dtls"
                                + " where pos_duty_dtls_seq = @Seq)";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);

                strSQL = "UPDATE HRM_MP_POSITION_DUTY_DTLS"
                           + " SET DESCR = @Descr"
                                + ", SEQ_NO = @No"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE POS_DUTY_DTLS_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(PositionDutyDtlsInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Seq", Info.Seq);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_POSITION_DUTY_DTLS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE POS_DUTY_DTLS_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
